#!/usr/bin/env python
# encoding: utf-8
"""
dj.py

Inspired by Sam Ruby's Venus code found here:

http://intertwingly.net/code/venus/

Created by Bud Gibson on 2010-06-04.
Copyright (c) Bud Gibson 2010. Released under the Artistic/GPL License.
"""

import os.path
from django.template import Context
from django.template.loader import get_template
try:
  from buzztracker.conf import DJANGO_TEMPLATES
except ImportError:
  sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
  from buzztracker.conf import DJANGO_TEMPLATES
  
def run(source, output_file='participantNetworks.html', template_file='postTemplate.html'):
  """Runs a source object (usually a dictionary) through a django template to produce an output. The template file is assumed to reside in a directory named templates."""
  from django.conf import settings
  try:
      settings.configure(
          DEBUG=True, TEMPLATE_DEBUG=True, 
          TEMPLATE_DIRS = (
            DJANGO_TEMPLATES,
            os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
          )
      )
  except EnvironmentError:
      print 'django settings blew up'
  

  # set up django template and render
  context = Context(source)
  t = get_template(template_file)
  # write out the rendered template
  f = open(output_file, 'w')
  f.write(t.render(context))
  f.close()
  